package com.wx.tnsbasedata.controller;

import com.wx.common.api.CommonResult;
import com.wx.tnsbasedata.model.CityModel;
import com.wx.tnsbasedata.model.JobModel;
import com.wx.tnsbasedata.model.vo.EntJobVo;
import com.wx.tnsbasedata.service.CityService;
import com.wx.tnsbasedata.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 林建权
 * @version 1.0
 * @date 2020/7/25 20:08
 */
@CrossOrigin
@RestController
@RequestMapping("/job")
@Api(tags = "职位数据")
public class JobController {
    @Autowired
    private JobService jobService;

    //前端需求接口

    //获取全部职业数据
    @GetMapping("/all")
    @ApiOperation(value = "获取全部职业数据", notes = "获取全部职业数据")
    public CommonResult<JobModel> findAll() {
        JobModel jobModel = null;
        try {
            jobModel = jobService.findAll();
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("获取职业数据失败！");
        }
        return CommonResult.success(jobModel);
    }

    //全部一级行业分类
    @GetMapping("/allTypeOne")
    @ApiOperation(value = "全部一级行业分类", notes = "全部一级行业分类")
    public CommonResult<List<EntJobVo>> allTypeOne() {
        List<EntJobVo> list = null;
        try {
            list = jobService.allTypeOne();
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("获取职业数据失败！");
        }
        return CommonResult.success(list);
    }

    //根据id查询职业或行业
    @GetMapping("/findById")
    @ApiOperation(value = "根据id查询职业或行业", notes = "根据id查询职业或行业")
    public CommonResult<JobModel> findById(@RequestParam Integer id) {
        System.out.println(id);
        JobModel jobModel = null;
        try {
            jobModel = jobService.findById(id);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("获取职业数据失败！");
        }
        return CommonResult.success(jobModel);
    }

    //测试Service接口
    @PostMapping("/insert")
    @ApiOperation(value = "添加职业", notes = "添加职业")
    public CommonResult insert(@RequestBody JobModel jobModel) {
        int result = 0;
        try{
            result = jobService.insert(jobModel);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("添加职业失败！");
        }
        return CommonResult.success("添加"+result+"条数据成功！");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改职业数据", notes = "修改职业数据")
    public CommonResult update(@RequestBody JobModel jobModel) {
        int result = 0;
        try{
            result = jobService.update(jobModel);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("修改职业数据失败！");
        }
        return CommonResult.success("修改"+result+"条数据成功！");
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除职业", notes = "删除职业")
    public CommonResult delete(@RequestParam Integer id) {
        int result = 0;
        try{
            result = jobService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.success("删除职业失败！");
        }
        return CommonResult.success("删除"+result+"条数据成功！");
    }
}
